<template>
  <el-dialog 
    class="simple-dialog"
    :title="tempData.id ? '修改' : '添加'"
    :visible.sync="tempVisible"
    width="400px"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="formName" :rules="rules" :model="tempData" label-width="120px">
      <!-- <el-form-item label="考试时间:" prop="ctime">
         <el-date-picker
          v-model="tempData.ctime"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择考试时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="发行商名称:" prop="name">
        <el-input v-model="tempData.name"/>
      </el-form-item>
      <el-form-item label="发行商首字母:" prop="nameCode">
        <el-input v-model="tempData.nameCode" maxlength="4" />
      </el-form-item> 
    </el-form>
    <span slot="footer" class="dalog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="saveUpdate('formName')">保存</el-button>
    </span>
  </el-dialog>
</template>

<script> 
  import testsApi from '@/api/system/publisher'
  export default {
    name: 'pubilsherAddEdit',
    props: {
      dialogVisible: {
        type: Boolean,
        default: false
      },
      data: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        tempVisible: false,
        tempData: {
          id: null
        },
        rules: {
          name: [{ required: true, message: '请输入发行商名称', trigger: 'blur' }],
          nameCode: [{ required: true, message: '请输入发行商首字母', trigger: 'blur' }],
        },
        areaInfo:[]
      }
    },
    watch: {
      dialogVisible: function(val) {
        this.tempVisible = val;
      },
      data: function(val) {
        this.tempData = JSON.parse(JSON.stringify(val));
      }
    },
    mounted() {
    },
    methods: {
      close() {
        this.$refs['formName'].clearValidate()
        this.$emit('close')
      },
      saveUpdate(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.tempData.nameCode.length != 4){
              this.$message.error('必须为4位数')
              return 
            }
            if (this.tempData.id) {
              testsApi.update(this.tempData).then(res => {
                if(res.status == 200){
                  this.$message.success('更新成功')
                  this.close()
                  this.$emit('reloadTable',res.data)
                }else{
                  this.$message.success('该发行商存在卡号不可修改')
                }
              }).catch(() => {})
            } else {
              testsApi.save(this.tempData).then(res => {
                this.$message.success('添加成功')
                this.$emit('close');
                this.$emit('reloadTable',res.data)
              }).catch(() => {
              })
            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style lang="scss">

</style>
